package com.minyifang.wcspf.work.ui.widgets;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class WaveLoadingView extends View {

    private SweepGradient mSweepGradient;
    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    private float mDensity;
    private int mStrokeWidth;
    private float mAnimProgress;

    public WaveLoadingView(Context context) {
        this(context, null);
    }

    public WaveLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams(context);
        initPaint();
        startAnim();
    }

    private void startAnim() {
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(600);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.addUpdateListener(animation -> {
            mAnimProgress = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }

    private void initParams(Context context) {
        mDensity = context.getResources().getDisplayMetrics().density;
        mStrokeWidth = (int) (2 * mDensity);
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mSweepGradient = new SweepGradient(w / 2, h / 2, new int[]{
                Color.parseColor("#05fafa"),
                Color.parseColor("#008cff"),
                Color.parseColor("#05fafa")
        }, new float[]{0f, .5f, 1f});
        mPaint.setColor(Color.parseColor("#05fafa"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAlpha(255);
        mPaint.setShader(mSweepGradient);
        canvas.rotate(360 * mAnimProgress, mWidth / 2, mHeight / 2);
        int radius = mWidth / 2 - mStrokeWidth;
        float scale = (Math.abs(mAnimProgress - .5f) + .5f);
        float ringScale = .8f + .2f * scale;
        canvas.drawCircle(mWidth / 2, mHeight / 2, radius * ringScale, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAlpha((int) (255 * (1 - mAnimProgress)));
        mPaint.setShader(null);
        canvas.drawCircle(mWidth / 2, mHeight / 2, (radius - radius / 4) * mAnimProgress, mPaint);
    }
}
